"""
随堂练习：将网页 一个图片 通过浏览器访问显示出来
提示 ： Content-Type:image/jpeg
"""
from socket import *

sock = socket()
sock.bind(('0.0.0.0', 8888))
sock.listen(5)

conn, addr = sock.accept()
print("Connect from ", addr)

data = conn.recv(1024)
print(data.decode())

# 组织http响应格式
response = """HTTP/1.1 200 OK
Content-Type:image/jpeg

"""
response = response.encode()
# img = open('res/img.jpg', 'rb')
with open('res/img.jpg', 'rb') as f:
    tmp = f.read()
response += tmp

conn.send(response)

conn.close()
sock.close()
